﻿Imports System.Data.OleDb
Public Class frmThemGroup
#Region "Form Event"
    Public GroupId As Integer = -1
    Private Sub frmThemGroup_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            txtTenNhom.Text = ""
            txtGhiChu.Text = ""
            If GroupId <> -1 Then
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dtGroup As New DataTable
                Dim query As String = "Select GroupId,GroupName,Descriptions From [Group] Where GroupId=" & GroupId
                Dim adapter As OleDb.OleDbDataAdapter
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                adapter.Fill(dtGroup)
                dbConn.Close()
                txtTenNhom.Text = dtGroup.Rows(0)("GroupName")
                txtGhiChu.Text = dtGroup.Rows(0)("Descriptions")
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"




#End Region
#Region "Load Data"

#End Region
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If txtTenNhom.Text = "" Then
                EmptyData(txtTenNhom)
                Exit Sub
            End If
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            If GroupId = -1 Then
                query = String.Format("Insert Into [Group](GroupName,Descriptions) Values(N'{0}',N'{1}')", txtTenNhom.Text, txtGhiChu.Text)
            Else
                query = String.Format("Update [Group] Set GroupName=N'{0}',Descriptions=N'{1}' Where GroupId={2}", txtTenNhom.Text, txtGhiChu.Text, GroupId)
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtTenNhom.Text = ""
            txtGhiChu.Text = ""
            frmGroup.NhomSuDung_Load()
            If GroupId <> -1 Then
                GroupId = -1
                Me.Close()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtTenNhom.Text = ""
            txtGhiChu.Text = ""
            frmGroup.NhomSuDung_Load()
            GroupId = -1
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub txtTenNhom_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTenNhom.KeyUp
        Try
            txtTenNhom.BackColor = Color.White
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
End Class